package com.pig4cloud.pigx.shop.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.constant.enums.ShopUserTypeEnum;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.order.api.feign.RemoteOrderInfoService;
import com.pig4cloud.pigx.shop.user.api.dto.CustomToUserOrderSummaryDTO;
import com.pig4cloud.pigx.shop.user.api.dto.CustomToUserSummaryDTO;
import com.pig4cloud.pigx.shop.user.api.entity.ShopUser;
import com.pig4cloud.pigx.shop.user.api.vo.CustomToUserOrderSummaryVO;
import com.pig4cloud.pigx.shop.user.api.vo.CustomToUserSummaryVO;
import com.pig4cloud.pigx.shop.user.mapper.ShopUserSummaryMapper;
import com.pig4cloud.pigx.shop.user.service.ShopUserSummaryService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Service
@AllArgsConstructor
public class ShopUserSummaryServiceImpl extends ServiceImpl<ShopUserSummaryMapper, ShopUser>
		implements ShopUserSummaryService {

	private final RemoteOrderInfoService remoteOrderInfoService;

	/**
	 * 根据销售代表获取下级用户数量
	 * @param page
	 * @param customToUserSummaryDTO
	 * @return
	 */
	@Override
	public IPage<CustomToUserSummaryVO> getCustomToUserSummary(Page page,
			CustomToUserSummaryDTO customToUserSummaryDTO) {
		return baseMapper.getCustomToUserSummary(page, customToUserSummaryDTO);
	}

	/**
	 * 获取详情
	 * @param customToUserSummaryDTO
	 * @return
	 */
	@Override
	public List<CustomToUserSummaryVO> getCustomToUserDetail(CustomToUserSummaryDTO customToUserSummaryDTO) {
		return baseMapper.getCustomToUserDetail(customToUserSummaryDTO);
	}

	/**
	 * 获取销售代表订单信息
	 * @param customToUserOrderSummaryDTO
	 * @return
	 */
	@Override
	public List<CustomToUserOrderSummaryVO> getCustomToUserOrderSummary(
			CustomToUserOrderSummaryDTO customToUserOrderSummaryDTO) {
		List<CustomToUserOrderSummaryVO> result = new ArrayList<>();
		// 获取业代信息
		LambdaQueryWrapper<ShopUser> lqwShopUser = new LambdaQueryWrapper<>();
		lqwShopUser.eq(ShopUser::getShopUserType, ShopUserTypeEnum.SALES.getType());
		if (!StringUtils.isEmpty(customToUserOrderSummaryDTO.getNickName())) {
			lqwShopUser.like(ShopUser::getNickName, customToUserOrderSummaryDTO.getNickName());
		}
		List<ShopUser> customList = this.list(lqwShopUser);
		// 获取订单信息
		List<CustomToUserOrderSummaryVO> orderList = remoteOrderInfoService
				.getCustomToUserOrderSummary(customToUserOrderSummaryDTO, SecurityConstants.FROM_IN);
		customList.forEach(custom -> {
			CustomToUserOrderSummaryVO vo = null;
			Optional<CustomToUserOrderSummaryVO> customInfo = result.stream()
					.filter(p -> p.getNickName().equals(custom.getNickName())).findFirst();
			if (customInfo.isPresent()) {
				// 存在的场合
				vo = customInfo.get();
			}
			else {
				// 不存在的场合
				vo = new CustomToUserOrderSummaryVO();
				vo.setUserIds(new ArrayList<>());
				vo.setNickName(custom.getNickName());
				vo.setTotalOrderCount(0L);
				vo.setTotalOrderPrice(0L);
				result.add(vo);
			}
			vo.getUserIds().add(custom.getId());
			// 获取用户订单金额和数量
			Optional<CustomToUserOrderSummaryVO> customOrder = orderList.stream()
					.filter(p -> p.getSalesShopUserId().longValue() == custom.getId().longValue()).findFirst();
			if (customOrder.isPresent()) {
				// 存在当前用户订单的场合
				Long orderCount = vo.getTotalOrderCount();
				orderCount += customOrder.get().getTotalOrderCount();
				vo.setTotalOrderCount(orderCount);

				Long orderPrice = vo.getTotalOrderPrice();
				orderPrice += customOrder.get().getTotalOrderPrice();
				vo.setTotalOrderPrice(orderPrice);
			}
		});

		return result.stream().sorted(Comparator.comparing(CustomToUserOrderSummaryVO::getTotalOrderPrice).reversed())
				.collect(Collectors.toList());
	}

	/**
	 * 获取销售代表订单详情
	 * @param customToUserOrderSummaryDTO
	 * @return
	 */
	@Override
	public List<CustomToUserOrderSummaryVO> getCustomToUserOrderDetail(
			CustomToUserOrderSummaryDTO customToUserOrderSummaryDTO) {
		List<CustomToUserOrderSummaryVO> result = new ArrayList<>();

		// 获取业代信息
		LambdaQueryWrapper<ShopUser> lqwShopUser = new LambdaQueryWrapper<>();
		lqwShopUser.eq(ShopUser::getShopUserType, ShopUserTypeEnum.SALES.getType());
		lqwShopUser.in(ShopUser::getId, customToUserOrderSummaryDTO.getIds());
		List<ShopUser> customList = this.list(lqwShopUser);

		// 获取订单信息
		List<CustomToUserOrderSummaryVO> orderList = remoteOrderInfoService
				.getCustomToUserOrderSummary(customToUserOrderSummaryDTO, SecurityConstants.FROM_IN);
		customList.forEach(custom -> {
			CustomToUserOrderSummaryVO vo = new CustomToUserOrderSummaryVO();
			vo.setNickName(custom.getNickName());
			vo.setLoginAccount(custom.getLoginAccount());
			vo.setTotalOrderCount(0L);
			vo.setTotalOrderPrice(0L);
			result.add(vo);
			// 获取用户订单金额和数量
			Optional<CustomToUserOrderSummaryVO> customOrder = orderList.stream()
					.filter(p -> p.getSalesShopUserId().longValue() == custom.getId().longValue()).findFirst();
			if (customOrder.isPresent()) {
				// 存在当前用户订单的场合
				Long orderCount = vo.getTotalOrderCount();
				orderCount += customOrder.get().getTotalOrderCount();
				vo.setTotalOrderCount(orderCount);

				Long orderPrice = vo.getTotalOrderPrice();
				orderPrice += customOrder.get().getTotalOrderPrice();
				vo.setTotalOrderPrice(orderPrice);
			}
		});

		return result.stream().sorted(Comparator.comparing(CustomToUserOrderSummaryVO::getTotalOrderPrice).reversed())
				.collect(Collectors.toList());
	}

}
